﻿using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 体尺服务实现
    /// </summary>
    public class BodySizeService : IBodySizeService
    {
        /// <summary>
        /// 体尺仓储
        /// </summary>

        private readonly IBaseRepository<BodySize> repository;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="repository">体尺仓储</param>
        public BodySizeService(IBaseRepository<BodySize> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 添加体型
        /// </summary>
        /// <param name="bodySize"></param>
        /// <returns></returns>
        public async Task<int> AddBodySize(BodySize bodySize)
        {
            return repository.Add(bodySize);
        }

        /// <summary>
        /// 删除体型
        /// </summary>
        /// <param name="BodySizeId"></param>
        /// <returns></returns>

        public int DelBodySize(int BodySizeId)
        {
            var list = repository.GetValue(BodySizeId);
            list.IsDel = 1;
            return repository.Update(list);
        }

        /// <summary>
        /// 批量删除体型列表
        /// </summary>
        /// <param name="BodySizeIds"></param>
        /// <returns></returns>
        public int DelRangeBodySize(List<int> BodySizeIds)
        {
            foreach (var item in BodySizeIds)
            {
                var list = repository.GetInfoBy(item);
                list.IsDel = 1;
                repository.Update(list);
            }
            return 1;
        }

        /// <summary>
        /// 获取体型列表
        /// </summary>
        /// <param name="overBit"></param>
        /// <param name="SheepType"></param>
        /// <param name="BodySex"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<PageOutput<BodySize>> GetBodySize(string? overBit, int SheepType, int BodySex, int pageIndex, int pageSize)
        {
            var list = repository.GetAll().Where(c => c.IsDel == 0).ToList();
            if (!string.IsNullOrEmpty(overBit))
            {
                list = list.Where(c => c.OverBit == overBit).ToList();
            }
            //判断类型
            if (SheepType > 0)
            {
                list = list.Where(c => c.SheepType == SheepType).ToList();
            }
            if (BodySex > 0)
            {
                bool boolBodySex = BodySex == 1 ? true : false;
                list = list.Where(c => c.BodySex == boolBodySex).ToList();
            }
            var result = new PageOutput<BodySize>()
            {
                Total = list.Count(),
                PageSize = pageSize,
            };
            var entity = list.OrderByDescending(c => c.BodySizeId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            result.Data = entity;
            return result;
        }

        /// <summary>
        /// 获取体型信息
        /// </summary>
        /// <param name="BodySizeId"></param>
        /// <returns></returns>
        public BodySize GetFtBodySize(int BodySizeId)
        {
            return repository.GetValue(BodySizeId);
        }

        /// <summary>
        /// 更新体型信息
        /// </summary>
        /// <param name="bodySize"></param>
        /// <returns></returns>
        public async Task<int> UpdaBodySize(BodySize bodySize)
        {
            return repository.Update(bodySize);
        }
    }
}
